/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package justdoit.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import justdoit.Startup;
import justdoit.controller.TaskManager;
import justdoit.controller.LanguageManager;
import justdoit.controller.XMLManager;

/**
 * The dialog for login
 * @author Can
 */
public class LoginDialog extends JDialog {

    private String[] userNameList;
    //components
    private JLabel userNameLable;
    //private JTextField userNameInput;
    private JComboBox userNameComboBox;
    private JLabel passwordLable;
    private JPasswordField passwordInput;
    private JLabel previousUsers;
    private JPanel inputPanel;
    private JPanel actionPanel;
    private JButton okButton;
    private JButton cancelButton;
    GridLayout gridLayout = new GridLayout(3, 2, 5, 5);

    public LoginDialog(String[] userNameList) {
        this.userNameList = userNameList;
        initComponents();
    }

    private void initComponents() {
        userNameLable = new JLabel(LanguageManager.getString("label.login.username"));
        //userNameInput = new JTextField();
        userNameComboBox = new JComboBox(this.userNameList);
        passwordLable = new JLabel(LanguageManager.getString("label.login.password"));
        passwordInput = new JPasswordField();
        okButton = new JButton(LanguageManager.getString("button.login.login"));
        cancelButton = new JButton(LanguageManager.getString("button.cancel"));
        previousUsers = new JLabel(LanguageManager.getString("label.currentUsername"));
        okButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                okAction();
            }
        });
        cancelButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                cancelAction();
            }
        });

        inputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        actionPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

        inputPanel.setLayout(gridLayout);
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        inputPanel.add(userNameLable);
        //inputPanel.add(userNameInput);
        inputPanel.add(userNameComboBox);
        inputPanel.add(passwordLable);
        inputPanel.add(passwordInput);
        //inputPanel.add(previousUsers);

        actionPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        actionPanel.add(okButton);
        actionPanel.add(cancelButton);

        this.setLayout(new BorderLayout());
        this.add(BorderLayout.CENTER, inputPanel);
        this.add(BorderLayout.SOUTH, actionPanel);

        this.getRootPane().setDefaultButton(okButton);

        this.setSize(300, 190);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setModal(true);
        this.setTitle(LanguageManager.getString("title.login"));

    }

    private void okAction() {
        int result = TaskManager.login(userNameComboBox.getSelectedItem().toString(),
                passwordInput.getPassword());
        if (result == 0) {
            System.out.println("Login success!");
            if (Startup.getMainFrame() != null) {
                Startup.getMainFrame().dispose();
                LanguageManager.loadLanguage(TaskManager.getCurrentUser().getLocale());
                Startup.showMainFrame(TaskManager.getCurrentUser());
                // Startup.getMainFrame().getTaskListTable().refreshData();
                // Startup.getMainFrame().updateUserNameLabel();
            }
            this.dispose();
        } else {
            System.out.println("Login failed!");
        }
        System.out.println("UserName: " + userNameComboBox.getSelectedItem().toString());
        System.out.println("Password: " + Arrays.toString(passwordInput.getPassword()));
    }

    private void cancelAction() {
        this.dispose();
    }
}
